#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

// Flag vector to signal the end of the wins in the wins array
#define END_OF_WINS 0	

int columns;
int rows;
int last_move;
int total_time;
int player_1_time;
int last_move_time;

FILE *f;

// The Wins Lookup Table
char WINS_LOOKUP[12][4] = {"ggrr","grgr","rgrg","rrgg","grrg","rggr","ggbb","gbgb","bgbg","bbgg","gbbg","bggb"};


/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
and their point values

This function has to do 3 things:
1) figure out which player won - return positive or negative values
2) Figure out what point value they won with - arrangment of the peices
3) If two players have the same winning value - return 1 point
*/
int evalWin(char wins[150][4])
{

	int i = 0;
	int points = 0;
	int bestwin = 0;
	int worstwin = 0;
	int redpoints = 0;
	int bluepoints = 0;
	do{
		//printf("checking case %s\n", wins[i]);
		//check for tie - if there is nothing in the wins array
		if(wins[0][0] == '\0' || wins[0][0] == 0)
		{
			//printf("0");//no wins in array - no nothing in array
			points = 0;
			return 0;
		}
		else//we have at least one win - check it out
		{
		//first - check for the good wins - where the first two char's will match
			if(wins[i][0] == wins[i][1])//same colour first two pieces
			{

				if(wins[i][0] == 'r')
					{ 
						redpoints += 5;
						points = -5;
					}//we won
				else if(wins[i][0] == 'b')
					{
						bluepoints += 5;
						points = 5;
					}
				
				else if(wins[i][0] == 'g')//don't know who won yet
				{
					if(wins[i][2] == 'r')
					{ 
						redpoints += 5;
						points = -5;
					}//we won
					else 
					{
						bluepoints += 5;
						points = 5;
					}
				}
			}
			
			else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
			{
				
				if(wins[i][0] == 'r')
				{ 
					redpoints += 4;
					points = -4;
				}//we won
				else if(wins[i][0] == 'b') 
				{
					bluepoints += 4;
					points = 4;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{ 
						redpoints += 4;
						points = -4;
					}//we won	
					else if(wins[i][1] == 'b') 
					{
						bluepoints += 4;
						points = 4;
					}
						
				}
					
			}
			else //3 points will be awarded
			{
				if(wins[i][0] == 'r')
				{ 
					redpoints += 3;
					points = -3;
				}//we won
				else if(wins[i][0] == 'b') 
				{
					bluepoints += 3;
					points = 3;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{ 
						redpoints += 3;
						points = -3;
					}//we won
					else if(wins[i][1] == 'b')  
					{
						bluepoints += 3;
						points = 3;
					}
				}
			}
			
			
			
		}
		if(points > 0)
		{
			if(bestwin < points)
				bestwin = points;
		}
		else
			if(worstwin > points)
				worstwin = points;

		i++;
	}while(wins[i][0] != 0);

	
	if( redpoints - bluepoints > 0)
		points = worstwin;
	else if(redpoints - bluepoints == 0)
		points = 1;
	else
		points = bestwin;
	
	//printf( "%d\n", points);
	return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
/*
void readBoard(char board[columns+2][rows+1])
{
	int i, j;

	// Populate board array
	for (i = 0; i < columns+2; i++)
	{
		// Padding columns on left and right sides of board
		if (i == 0 || i == columns+1)
		{
			for(j = 0; j < rows+1; j++)
				board[i][j] = SPACE;
			continue;
		}
		for (j = 0; j < rows; j++)
		{
			scanf(",%c", &board[i][j]);
			printf("%c", board[i][j]);
		}
		// Padding row over top of board
		board[i][j] = SPACE;
	}
}
*/
void findWins(char board[columns+2][rows+1], char wins[150][4])
{
	int i, j, winIndex = 0;
	for (i = 1; i < columns+2; i++)
	{
		// For Each Piece
		for (j = 0; j < rows; j++) 
		{
			if (board[i][j] == SPACE)		
				continue;
			short int offset, size = 1;
			char win[4] = {board[i][j],'s','s','s'};

			// First check Up-Left
			for (offset = 1;offset < 4;offset++)
			{
				if (board[i-offset][j+offset] == SPACE)
					break;
					
				win[size++] = board[i-offset][j+offset];
			}
			if (size == 4) // Found a four piece vector (No Spaces)
			{
				
				//printf("UPLEFT: Found a 4 piece vector: %s\n", win);
				int k;
				for (k=0;k<12;k++)
				{
					if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
					{
						//printf("\t^Found a Win!\n");
						strcpy(wins[winIndex++], win);
					}
				}
				
			}

			// Next check Straight Up
			size = 1;
			
			for (offset = 1;offset < 4;offset++)
			{
				if (board[i][j+offset] == SPACE)
					break;
					
				win[size++] = board[i][j+offset];
			}
			if (size == 4) // Found a four piece vector (No Spaces)
			{
				//printf("UP: Found a 4 piece vector: %s\n", win);
				int k;
				for (k=0;k<12;k++)
				{
					if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
					{
						//printf("\t^Found a Win!\n");
						strcpy(wins[winIndex++], win);
					}
				}
			}
			

			// Next check Up-Right
			size = 1;
			for (offset = 1;offset < 4;offset++)
			{
				if (board[i+offset][j+offset] == SPACE)
					break;
					
				win[size++] = board[i+offset][j+offset];
			}
			if (size == 4) // Found a four piece vector (No Spaces)
			{
				//printf("UPRIGHT: Found a 4 piece vector: %s\n", win);
				int k;
				for (k=0;k<12;k++)
				{
					if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
					{
						//printf("\t^Found a Win!\n");
						strcpy(wins[winIndex++], win);
					}
				}
			}

			// Finally check Straight Right
			size = 1;
			for (offset = 1;offset < 4;offset++)
			{
				if (board[i+offset][j] == SPACE)
					break;
					
				win[size++] = board[i+offset][j];
			}
			if (size == 4) // Found a four piece vector (No Spaces)
			{
				//printf("RIGHT: Found a 4 piece vector: %s\n", win);
				int k;
				for (k=0;k<12;k++)
				{
					if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
					{
						//printf("\t^Found a Win!\n");
						strcpy(wins[winIndex++], win);
					}
				}
			}
		}
		
	}
	// End with "end of wins" flag vector, to signal the end of wins
	wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+1])
{
	int i, j;
	// TEST: Print out board
	for (i = 0; i < columns+2; i++)
	{
		for (j = 0; j < rows+1; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int main( int argc, char **argv)
{
	int i, j;
	char board[10][8];
	char buf[128];
	f = fopen("testboards.txt", "r");
	int points = 0;
	int points2 = 0;
	char value[8];
	
	do{
		//printf("opened");
		fscanf(f, "(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
		// Populate board array
		for (i = 0; i < columns+2; i++)
		{
			// Padding columns on left and right sides of board
			if (i == 0 || i == columns+1)
			{
				for(j = 0; j < rows+1; j++)
					board[i][j] = SPACE;
				continue;
			}
			for (j = 0; j < rows; j++)
			{
				fscanf(f,",%c", &board[i][j]);
				//printf("%c", board[i][j]);
			}
			// Padding row over top of board
			board[i][j] = SPACE;
		}
		printf("\n");
		fgetc(f);
		fgetc(f);//get the space - leave it out
		
		i = 0;
		value[i] = fgetc(f);//get first char
		//printf("value is: %c\n", value[i]);
		
		if(value[i] == '-')//if it is a negative sign, get the next one
		{
			i++;
			value[i] = fgetc(f);//get second char
			//printf("value is:%c\n", value[i]);
			i++;
			value[i] = '\0';//mark the end
			points = ('0' - value[1]);
			
		}
		
		else
		{
			value[1] = '\0';//mark the end
			points = 0 - ('0' - value[0]);
			
		}
		
		//printf("\n\n***** %d\n\n", points);
		
		//get the rest of the line and do nothing with it. Should only be a few bytes for the value of the win
		fgets(buf, 127, f); 
		
		// How many possible simultaneous wins can there be?
		char wins[150][4];
	    
	    // For Debugging Only
		//char test[10];
		
		//readBoard(board);
	
		//printf("Done ReadBoard!\n");
		//scanf("%s", &test);
		//printBoard(board);
		//printf("Done PrintBoard!\n");
		//scanf("%s", &test);
		findWins(board, wins);
		//printf("Done findWins!\n");
		//scanf("%s", &test);
		points2 = evalWin(wins);
		if(points == points2)
			printf("PASS -> %d = %d\n", points, points2);
		else
			printf("\n\n**************   FAIL %d != %d   ****************\n\n", points, points2);
		//printf("Done evalWin!\n");
		//scanf("%s", &test);
	}while(!feof(f));
	fclose(f);
	
}

